package viewbuilder.java;

import groovy.xml.MarkupBuilder;
import org.junit.BeforeClass;
import org.junit.Test;
import util.StopWatch;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 */
public class PerformanceTest {
    static Customer customer;
    static List<CustomerUser> users;

    @BeforeClass
    public static void initCustomerAndGroovyCaches() {
        customer = new Customer(); customer.longName = "Tescos";
        users = new ArrayList<CustomerUser>();
        Random random = new Random(10);
        for(int i=0; i<300; i++) {
            int val = random.nextInt();
            users.add(new CustomerUser(val, "first"+val, "second"+val, "user"+val));
        }

        System.out.println("--- ignore start");
        //the point of this is to pre-populate the Groovy MetaClassRegistry cache.
        //it looks like the cache is stored on thread local, which will not help us for web requests!
        StopWatch stopwatch = new StopWatch();
        stopwatch.start();
        new GroovyTable(new MarkupBuilder(new StringWriter())).renderCustomerUserTable( customer, users );
        stopwatch.stop();
        long time = stopwatch.getDurationInMillis();
        System.out.println("The startup cost is: " + time);
        System.out.println("--- ignore end");
    }


    @Test
    public void whichIsFaster() {
        pureJavaBuilder();
        markupBuilder();
        htmlBuilderJava();
        htmlBuilderGroovy();
        pureJavaBuilder();

        new Thread() {
            public void run() {
                markupBuilder();
            }
        }.run();
    }


//    @Test
    public void htmlBuilderJava() {
        StopWatch stopwatch = new StopWatch();
        StringWriter writer = new StringWriter();
        stopwatch.start();
        new GroovyTable(new viewbuilder.HtmlBuilderJava(writer)).renderCustomerUserTable( customer, users );
        stopwatch.stop();
        long time = stopwatch.getDurationInMillis();
        System.out.println("The Java impl of GroovyHtml builder test took: " + time);
//        return time;
    }

//    @Test
    public void htmlBuilderGroovy() {
        StopWatch stopwatch = new StopWatch();
        StringWriter writer = new StringWriter();
        stopwatch.start();
        new GroovyTable(new viewbuilder.HtmlBuilder(writer)).renderCustomerUserTable( customer, users );
        stopwatch.stop();
        long time = stopwatch.getDurationInMillis();
        System.out.println("The Groovy HtmlBuilder test took: " + time);
//        return time;
    }

    
//    @Test
    public void markupBuilder() {
        StopWatch stopwatch = new StopWatch();
        StringWriter writer = new StringWriter();
        stopwatch.start();
        new GroovyTable(new MarkupBuilder(writer)).renderCustomerUserTable( customer, users );
        stopwatch.stop();
        long time = stopwatch.getDurationInMillis();
        System.out.println("The Groovy Markup test took: " + time);
//        return time;
    }



//    @Test
    public void pureJavaBuilder() {
        StopWatch stopwatch = new StopWatch();
        stopwatch.start();
        new JavaTable(customer, users).render();
        stopwatch.stop();
        long time = stopwatch.getDurationInMillis();
        System.out.println("The pure Java builder test took: " + time);
//        return time;
    }


}

class Customer {
    String longName;
}

class CustomerUser {
    Integer id;
    String firstName;
    String lastName;
    String username;

    CustomerUser( Integer id, String firstName, String lastName, String username ) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.username = username;
    }
}

